package com.zerui.manager.iot.conf;


import com.zerui.cola.dto.MultiResponse;
import com.zerui.devicemanager.provider.DeviceDubboService;
import com.zerui.devicemanager.provider.dto.ProductTreeDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.List;
import java.util.Map;

/**
 * IoT远程字典配置
 */
@Configuration
@Order(value = 16)
public class IOTDictRemoteConf implements CommandLineRunner {

    private final Map<String, String> productsMap = new ConcurrentHashMap<>();

    @DubboReference
    private DeviceDubboService deviceDubboService;

    @Override
    public void run(String... args) throws Exception {
        // 读取产品
        MultiResponse<ProductTreeDTO> productRepo = deviceDubboService.getProductList();
        List<ProductTreeDTO> data = productRepo.getData();
        for (ProductTreeDTO dto : data) {
            dto.getProducts().forEach(c -> {
                productsMap.put(c.getProductCode(), c.getProductName());
            });
        }
    }

    public String getProductName(final String productCode) {
        if (StringUtils.isEmpty(productCode)) return null;
        return productsMap.get(productCode);
    }
}
